package com.keldian.mobilesafe.engine;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.keldian.mobilesafe.domain.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-1-12.
 */
public class AppInfoProvider {


    public static List<AppInfo> getAppInfo(Context context) {
        List<AppInfo> list = new ArrayList<AppInfo>();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packageInfos = pm.getInstalledPackages(0);

        for (PackageInfo packageInfo : packageInfos) {
            AppInfo appInfo = new AppInfo();
            String packname = packageInfo.packageName;
            //应用程序的信息标记
            int flags = packageInfo.applicationInfo.flags;
            int uid = packageInfo.applicationInfo.uid;

            if ((flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                appInfo.setUserApp(true);
            } else {
                appInfo.setUserApp(false);
            }
            if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0) {
                appInfo.setInRom(true);
            } else {
                appInfo.setInRom(false);
            }

            Drawable icon = packageInfo.applicationInfo.loadIcon(pm);

            String name = packageInfo.applicationInfo.loadLabel(pm).toString();
            appInfo.setUid(uid);
            appInfo.setIcon(icon);
            appInfo.setPackname(packname);
            appInfo.setName(name);
            list.add(appInfo);
        }

        return list;
    }


}
